package com.tcm.order.service.task;

import cn.hutool.core.util.NumberUtil;
import com.base.genid.generator.UidGenerator;
import com.tcm.core.service.ConfigService;
import com.tcm.doctor.service.DoctorService;
import com.tcm.order.domain.dto.OrderBillTaskDTO;
import com.tcm.order.domain.dto.OrderStoreBillDTO;
import com.tcm.order.entity.OrderStoreBill;
import com.tcm.order.dao.mapper.OrderStoreBillMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
public class OrderStoreBillTask implements OrderBillTaskFactory {

    @Autowired
    private OrderStoreBillMapper orderStoreBillMapper;

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, retries = 3, check = false)
    private ConfigService configService;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", timeout = 6000, check = false, retries = 3)
    private UidGenerator uidGenerator;

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, retries = 3, check = false)
    private DoctorService doctorService;

    @Override
    public void create(OrderBillTaskDTO order) {

        OrderStoreBill orderStoreBill = new OrderStoreBill();

        orderStoreBill.setId(uidGenerator.getUID());
        orderStoreBill.setOrderId(order.getId());
        orderStoreBill.setStoreId(order.getStoreId());
        orderStoreBill.setOrderTotals(order.getTotalMoney());
        orderStoreBill.setDrugMoney(order.getDrugMoney());
        orderStoreBill.setQuantity(order.getQuantity());
        orderStoreBill.setDeductPointMoney(order.getDeductPointMoney());
        orderStoreBill.setInviterCommission(order.getInviterMoney());
        orderStoreBill.setTotalDrugsMoney(order.getTotalDrugMoney());
        orderStoreBill.setTotalServiceMoney(order.getTotalServiceMoney());
        orderStoreBill.setDecoctingFee(order.getDecoctingFee());
        orderStoreBill.setProcessCost(order.getProcessCost());
        orderStoreBill.setLogisticsFee(order.getLogisticsFee());

        //药房药费结算比例
        BigDecimal settlement_pharmacy = new BigDecimal(configService.findValue("settlement_pharmacy", "0.25"));
        orderStoreBill.setDrugsCommission(NumberUtil.mul(settlement_pharmacy, orderStoreBill.getTotalDrugsMoney()));
        BigDecimal totalMoney = NumberUtil.add(
                orderStoreBill.getDrugsCommission(),
                orderStoreBill.getDecoctingFee(),
                orderStoreBill.getLogisticsFee(),
                orderStoreBill.getProcessCost());
        orderStoreBill.setTotalMoney(
                NumberUtil.sub(totalMoney, order.getDeductPointMoney())
        );
        orderStoreBill.setCreateTime(LocalDateTime.now());
        orderStoreBill.setUpdateTime(LocalDateTime.now());
        orderStoreBill.setState("0");
        orderStoreBillMapper.insert(orderStoreBill);
    }

}
